package com.opennetexam.manager.controller;

import com.opennetexam.manager.bussinessservice.ExamPaperService;
import com.opennetexam.manager.bussinessservice.ExamPlanInfoService;
import com.opennetexam.manager.common.*;
import com.opennetexam.manager.databasemodel.*;
import com.opennetexam.manager.dto.*;
import com.opennetexam.manager.enums.ParameterKey;
import com.opennetexam.manager.utils.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/13.
 */
@Controller
@RequestMapping(value = "/ExamPlan")
public class ExamPlanController extends BaseController{

    @Autowired
    private ExamPlanInfoService examPlanInfoService;
    @Autowired
    private ExamPaperService examPaperService;

    @Value("${ExamSiteURL}")
    private String examSiteURL;


    /**
     * 获取考试计划列表
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetList",method = RequestMethod.POST)
    public void getList(@RequestBody Map<String,Object> requestBody, HttpServletRequest request, HttpServletResponse response){
        //ExamPlanType考试计划类别：0正式；1模拟
        Integer planType = (Integer) requestBody.get("planType");
        String examPlanName = (String) requestBody.get("examPlanName");
        String account = (String) requestBody.get("account");
        ExamAdminInfoDTO adminUserInfo = getAdminUserInfo(account);
        List<ExamPlanViewModel> examPlanList = examPlanInfoService.getExamPlanList(adminUserInfo.getAppID(), adminUserInfo.getExamSiteID(), planType.shortValue(),examPlanName);
        CommonFunction.responseWrite(response,examPlanList);
    }

    /**
     * 获取所有的科目列表，需要生成的试卷
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetPaperList",method = RequestMethod.POST)
    public void getPaperList(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response){
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        String account = (String) requestBody.get("account");
        List<ExamPaperCount> examSubject = examPaperService.getExamSubject(planId, getAdminUserInfo(account).getAppID());
        CommonFunction.responseWrite(response,examSubject);
    }

    /**
     * 获取当前考点下批次列表
     */
    @RequestMapping(value = "/GetBatchList",method = RequestMethod.POST)
    public void getBatchList(String account,HttpServletResponse response){
        CommonFunction.responseWrite(response,examPlanInfoService.getBatchList(getAdminUserInfo(account)));
    }

    /**
     * 分页查询当前计划下考试科目列表
     * @param requestBody
     * @param response
     */
    @RequestMapping(value = "/findExamSubjectList")
    public void findExamSubjectList(@RequestBody Map<String,Object> requestBody,HttpServletResponse response){
        String examPlanID = requestBody.get("examPlanID").toString();
        Integer currentPage = Integer.parseInt(requestBody.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(requestBody.get("pageSize").toString());
        PageBean<ExamSubject> pageFindAllExamSubject = examPlanInfoService.findAllExamSubjectByExamPlanID(examPlanID, currentPage, pageSize);
        CommonFunction.responseWrite(response,pageFindAllExamSubject);
    }

    /**
     * 判断是否需要座位号
     * @param requestBody
     * @param response
     */
    @RequestMapping(value = "/IsRequireSetSeat",method = RequestMethod.POST)
    public void isRequireSetSeat(@RequestBody Map<String,Object> requestBody, HttpServletResponse response){
        String account = (String) requestBody.get("account");
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        OperationResult operationResult = examPaperService.isSeatSet(planId, getAdminUserInfo(account).getAppID());
        CommonFunction.responseWrite(response,operationResult);
    }

    /**
     * 获取场次列表
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetSceneList",method = RequestMethod.POST)
    public void getSceneList(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response){
        String account = (String) requestBody.get("account");
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        String roomId = (String) requestBody.get(ParameterKey.ROOMID.getDisplay());
        String subjectId = (String) requestBody.get(ParameterKey.SUBJECTID.getDisplay());

        List<ExamScenes> examScenes = examPlanInfoService.examSceneList(getAdminUserInfo(account).getAppID(), planId, roomId, subjectId);
        CommonFunction.responseWrite(response,examScenes);
    }

    /**
     * 获取考场列表
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetRoomList",method = RequestMethod.POST)
    public void getRoomList(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response){
        String account = (String) requestBody.get("account");
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        String sceneId = (String) requestBody.get(ParameterKey.SCENEID.getDisplay());
        String subjectId = (String) requestBody.get(ParameterKey.SUBJECTID.getDisplay());

        List<ExamRoom> examRooms = examPlanInfoService.examRoomList(getAdminUserInfo(account).getAppID(), planId, sceneId, subjectId);
        CommonFunction.responseWrite(response,examRooms);
    }

    /**
     * 获取科目列表
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetSubjectList",method = RequestMethod.POST)
    public void getSubjectList(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response){
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());

        List<ExamSubject> examSubjects = examPlanInfoService.examSubjectList(planId);
        CommonFunction.responseWrite(response,examSubjects);
    }

    /**
     * 根据场次ID、考场ID、科目ID获取考试具体数据
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetDetailList",method = RequestMethod.POST)
    public void getDetailList(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response){
        String account = (String) requestBody.get("account");
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        String sceneId = (String) requestBody.get(ParameterKey.SCENEID.getDisplay());
        String roomId = (String) requestBody.get(ParameterKey.ROOMID.getDisplay());
        String subjectId = (String) requestBody.get(ParameterKey.SUBJECTID.getDisplay());
        ExamAdminInfoDTO adminUserInfo = getAdminUserInfo(account);

        List<ExamPlanDetailViewModel> detailViewModels = examPlanInfoService.selectbyExamSubject(planId, roomId, sceneId, subjectId, adminUserInfo.getAppID(), adminUserInfo.getExamSiteID());
        CommonFunction.responseWrite(response,detailViewModels);
    }

    /**
     * 获取监考状态
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetMoniterStatus",method = RequestMethod.POST)
    public void getMoniterStatus(@RequestBody Map<String,Object> requestBody,HttpServletRequest request,HttpServletResponse response){
        //考试计划ID
        String planId = (String) requestBody.get(ParameterKey.PLANID.getDisplay());
        //科目ID
        String subjectId = (String) requestBody.get(ParameterKey.SUBJECTID.getDisplay());
        String account = (String) requestBody.get("account");

        OperationResult moniterStatus = examPaperService.getMoniterStatus(getAdminUserInfo(account).getAppID(), planId, subjectId);
        CommonFunction.responseWrite(response,moniterStatus);
    }

    /**
     * 获取考点计划包
     * @param examSiteID
     * @return 下载的文件存放路径
     */
    private Map<String,Object> downLoadExamPlanPackage(String examSiteID){
        Map<String,Object> returnMap = new HashMap<>();
        String url = examSiteURL + "examplan/pack?exampointId="+examSiteID;
        String filePath = ExamSitePath.rootPath;
        String fileName = System.currentTimeMillis() + ".oepp";
        String result = HttpRequestUtils.sendGet(url, null);
        ReturnData returnData = GsonUtil.fromJsonToObject(result, ReturnData.class);
        if(returnData.getSuccess() && !StringUtils.isEmpty(returnData.getPayload().toString())){
            String base64 = returnData.getPayload().toString();
            byte[] bytes = Base64.decodeBase64(base64);
            //生成文件
            FileUtil.getFile(bytes,filePath,fileName);
            returnMap.put("filePath",filePath + "/" + fileName);
        }
        return returnMap;
    }

    public static void main(String[] args) {
        String fileContent = FileUtil.getFileContent("C:\\Users\\devsun\\Desktop\\123.txt");
        byte[] bytes = Base64.decodeBase64(fileContent);
        //生成文件
        FileUtil.getFile(bytes,"C:/Users/devsun/Desktop","1.zip");
    }
}
